package com.controller.sys.elastic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.DateUtils;
import com.common.utils.EntityConversionUtil;
import com.github.pagehelper.PageInfo;
import com.model.base.PageResult;
import com.model.base.Result;
import com.model.domain.device.GetDeviceObdBaseInfoDO;
import com.model.dto.device.deviceOBDInfo.GetDeviceListDTO;
import com.model.dto.sys.elastic.ElasticDto;
import com.model.pojo.device.DeviceObdBaseInfo;
import com.model.vo.device.deviceOBDInfo.getDeviceList.GetDeviceListVO;
import com.model.vo.sys.elastic.ElasticListVO;
import com.service.device.DeviceObdBaseInfoService;
import com.service.sys.ElasticService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(tags = {"Elastic日志查询"})
@Slf4j
@RestController
@RequestMapping("/v1/elastic")
@RequiredArgsConstructor
public class ElasticController {

    private final ElasticService elasticService;

    @Log(title = "ELastic日志查询列表", businessType = BusinessType.INSERT)
    @ApiOperation(value = "日志列表")
    @PostMapping("/list")
    public Result<PageResult<List<ElasticListVO>>> getElasticLog(@RequestBody @Valid ElasticDto dto){
        List<ElasticListVO> list=new ArrayList<>();
        int totalCount=0;
        JSONObject object=elasticService.query(dto.getDtu(),dto.getPage(),dto.getPageSize(),dto.getIndex(),dto.getStartTime(),dto.getEndTime());
        if(object!=null){
            JSONObject hits=object.getJSONObject("hits");
            JSONObject total=hits.getJSONObject("total");
            totalCount=total.getInteger("value");//总数
            JSONArray array=hits.getJSONArray("hits");//拿到的数据

            for(int i=0;i<array.size();i++){
                JSONObject item=array.getJSONObject(i);
                JSONObject source=item.getJSONObject("_source");
                long date=source.getLong("date");
                String dtu=source.getString("dtu");
                String type=source.getString("type");
                String value=source.getString("value");
                String d=DateFormatUtils.format(date, DateUtils.YYYY_MM_DD_HH_MM_SS);
                ElasticListVO vo=new ElasticListVO();
                vo.setDate(d);
                vo.setDtu(dtu);
                vo.setType(type);
                vo.setValue(value);
                list.add(vo);
            }
        }

        PageResult result=new PageResult();
        result.setResult(list);
        PageInfo pageInfo=new PageInfo();
        pageInfo.setPageSize(dto.getPageSize());
        pageInfo.setPageNum(dto.getPage());
        pageInfo.setPages(totalCount/dto.getPageSize());
        pageInfo.setTotal(totalCount);
        result.setPageInfo(pageInfo);

        return Result.suc(result);
    }
}
